﻿using System;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;
using System.Text;
using SdmxMl.GenericMetadata;

namespace SmsMeta.StructureRequest
{
    public class MetadataSetRequest : BaseArtefactRequest
    {
        public MetadataSetRequest(QueryHelper qh)
            : base(qh)
        {
        }
        public override XmlDocument GetSdmxMl()
        {
            XmlDocument xDoc = new XmlDocument();
            SmManager sm = new SmManager();

            ArtefactRefTypedList artl = GetMatching(queryHlp);
            if (artl.Count > 1 || artl.Count == 0 ||
                (artl.Count == 1 && queryHlp.QueryDetail != ArtefactDetail.Full))
            {
                // Only a list can be provided inside a metadataset
                sm.Clear();
                sm.PutArtefact(SdmxArtefactType.MetadataSet, CreateListOfMetaSet(artl));

            }
            else
            {
                sm.LoadMissingArtefactsFromRegistry(artl);
            }

            xDoc = sm.GetMetasetAsDocument(artl[0].FullIdent);

            return xDoc;
        }


        private MetadataSetType CreateListOfMetaSet(ArtefactRefTypedList artl)
        {
            MetadataSetType metaSet = new MetadataSetType();
            metaSet.Id = "LIST_METADATASET";
            metaSet.AgencyId = "NSI";
            metaSet.Name.UpdateDefaultText("List of metadataset located in Repository");
            metaSet.MetadataStructureRef.Id = "MSD_METASET_LIST";
            metaSet.MetadataStructureRef.AgencyId = "NSI";
            metaSet.ReportRef = "R_LIST";

            int key = 0;
            foreach (ArtefactRefTyped art in artl)
            {
                AttributeValueSetType at = new AttributeValueSetType(metaSet);
                at.TargetValuesTypeList.Clear();
                // The target values
                ComponentValueType cvt = new ComponentValueType();
                cvt.Component = "key";
                cvt.Object = ObjectTypeCodelistType.Code;
                cvt.SimpleValue = (++key).ToString();
                at.TargetValuesTypeList.Add(cvt);

                // The reported attrib
                ReportedAttributeType ra = new ReportedAttributeType();
                ra.ConceptID = "AGENCY_ID";
                ra.Value = new MultiLang(art.AgencyId);
                at.ReportedAttributeList.Add(ra);

                ra = new ReportedAttributeType();
                ra.ConceptID = "ID";
                ra.Value = new MultiLang(art.Id);
                at.ReportedAttributeList.Add(ra);

                ra = new ReportedAttributeType();
                ra.ConceptID = "VERSION";
                ra.Value = new MultiLang(art.Version);
                at.ReportedAttributeList.Add(ra);

                ra = new ReportedAttributeType();
                ra.ConceptID = "NAME";
                ra.Value = art.Name.Clone();
                at.ReportedAttributeList.Add(ra);

                metaSet.AttributeValueSetList.Add(at);
            }

            return metaSet;
        }
    }
}
